package Tool;

import Javabean.Staff;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;

public class Function_Staff_Search_ID {
    public Function_Staff_Search_ID(Connection conn,JTable table,String[] columnNames) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        new InitJdialog(jDialog);
        jDialog.setSize(350,150);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        jDialog.setTitle("ID查询");

        //设置组件
        JTextField idtxt = new JTextField();

        JLabel id = new JLabel("ID");

        JButton truejbt = new JButton("确定");


        id.setBounds(20,12,70,20);

        idtxt.setBounds(80,15,200,20);

        truejbt.setBounds(130,60,70,30);


        //添加组件
        jDialog.getContentPane().add(id);

        jDialog.getContentPane().add(idtxt);

        jDialog.getContentPane().add(truejbt);

        //添加监听
        truejbt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //获取输入数据
                    String id = idtxt.getText();

                    //创建查找集合
                    ArrayList<Staff> searchstaff = new ArrayList<>();
                    //在数据库中查找
                    MySQL_StaffSearch.staffIdSearch(conn,searchstaff,id);
                    //更新表格数据
                    String[][] tableValues = new String[searchstaff.size()][columnNames.length];
                    tableValues = Init_Staff_TableRefresh.stafftableRefresh(searchstaff);
                    TableModel dataModel = new DefaultTableModel(tableValues, columnNames);
                    table.setModel(dataModel);

                } catch (Exception ex) {

                    throw new RuntimeException(ex);

                }

                //关闭弹窗
                jDialog.dispose();
            }
        });

        //让弹框显示出来
        jDialog.setVisible(true);
    }
}
